package com.lw.leetcode.arr;

import java.util.ArrayList;
import java.util.List;

/**
 * 1424. 对角线遍历 II
 *
 * @Author liw
 * @Date 2021/5/21 17:15
 * @Version 1.0
 */
public class FindDiagonalOrder {

    public int[] findDiagonalOrder(List<List<Integer>> nums) {
        int length = nums.size();
        int max = length;
        for (int i = 0; i < length; i++) {
            max = Math.max(max, nums.get(i).size() + i);
        }
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < max; i++) {
            int st = i < length ? 0 : i - length + 1;
            for (int j = st; j <= i; j++) {
                List<Integer> item = nums.get(i - j);
                if ( j >= item.size()) {
                    continue;
                }
                list.add(item.get(j));
            }
        }
        int[] arr = new int[list.size()];
        int index = 0;
        for (Integer value : list) {
            arr[index++] = value;
        }
        return arr;
    }

}
